﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class UserManage_ChangePassword : BasePage
{
    IRolePurviewHandler userManage = new RolePurviewHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1010"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }           
            dataBind();
        }
    }

    private void dataBind()
    {
       
        if (Session["UserID"] != null)
        {
            UsersQuery query = userManage.GetUsers(int.Parse(Session["UserID"].ToString()));
            if (query != null)
            {
                txtLoginID.Text = query.LoginID;             
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
        {
            UsersQuery usersQuery = userManage.GetUsers(int.Parse(Session["UserID"].ToString()));
            if (usersQuery != null)
            {

                if (StringUtil.EncryptPassword(txtOldPassword.Text.Trim(), "MD5") != usersQuery.Password)
                {
                    Jscript.Alert(Resources.Messages.OldPasswordError, this.Page);
                    return;
                }
                else
                {

                    usersQuery.Password = StringUtil.EncryptPassword(txtNewPassword.Text.Trim(),"MD5");
                   
                    try
                    {
                        if (userManage.UserUpdate(usersQuery))
                        {
                            Jscript.Alert(Resources.Messages.ChangePasswordSuccessed, this.Page);
                        }
                        else
                        {
                            Jscript.Alert(Resources.Messages.ChangePasswordFailed, this.Page);
                        }
                    }
                    catch
                    {
                        Jscript.Alert(Resources.Messages.ChangePasswordFailed, this.Page);
                    }
                }
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}
